<?php
if (!isset($_SESSION)) {
    session_start();
}
class tourandteam extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    protected $configs;

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('smarty3');
        $this->load->model('pushclimb/tour_model');
        $this->load->model('admin-planners/setting_model');
        $this->load->model('pushclimb/type_model');
        include APPPATH . 'libraries/defu.php';
        $this->smarty=new CI_Smarty3();
        $this->setting_model=new setting_model();
        $this->tour_model=new tour_model();
        $this->type_model= new type_model();
    }

    function Init() {
        $data = $this->setting_model->gets();
        foreach ($data as $v) {
            $_SESSION["SETTINGS"][$v->Key] = $v->Value;
        }
    }

    public function index() {
        $Data=null;
        $Data["tourtype"]=$this->type_model->getByType("company");
        if(count($Data["tourtype"])>0){
            redirect(base_url("tourandteam/company/".$Data["tourtype"][0]->_alias));
        }else show_404 ();
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/09_list_tour");
    }
    public function company($alias="") {
        $Data=null;
        $Data["tourtype"]=$this->type_model->getByType("company");
        if(count($Data["tourtype"])>0){
            foreach($Data["tourtype"] as $t){
                if($t->_alias==$alias)
                    break;
            }
            $Data["tours"]=$this->tour_model->getByType($t->_no);
        }else show_404 ();
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/09_list_tour");
    }
    public function detail($alias="") {
        $Data=null;
        $Data["tourtype"]=$this->type_model->getByType("company");
        $Data["tourindex"]=$this->tour_model->getByAlias($alias);
        if(isset($Data["tourindex"][0]))$Data["tourindex"]=$Data["tourindex"][0];
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/06_tour");
    }
    function book(){
        $msgs=array();
        if( (!isset($_POST["Tour"])) || $_POST["Tour"]==""){
            $msgs[]="Tour does not empty.";
        }
        if( (!isset($_POST["FullName"])) || $_POST["FullName"]==""){
            $msgs[]="FullName does not empty.";
        }
        if( (!isset($_POST["Age"])) || $_POST["Age"]==""){
            $msgs[]="Age does not empty.";
        }
        if( (!isset($_POST["Num"])) || $_POST["Num"]==""){
            $msgs[]="Num does not empty.";
        }
        if( (!isset($_POST["Date"])) || $_POST["Date"]==""){
            $msgs[]="Date does not empty.";
        }
        if( (!isset($_POST["Location"])) || $_POST["Location"]==""){
            $msgs[]="Location does not empty.";
        }
        if( (!isset($_POST["Email"])) || $_POST["Email"]==""){
            $msgs[]="Email does not empty.";
        }
        if( (!isset($_POST["Phone"])) || $_POST["Phone"]==""){
            $msgs[]="Phone does not empty.";
        }
        
        if(count($msgs)>0){
            $code=-44;
            $msg="";
            foreach ($msgs as $m){
                $msg.="$m<br/>";
            }
        }else{
            $Params=array(
                "Tour"=>$_POST["Tour"],
                "FullName"=>$_POST["FullName"],
                "Age"=>$_POST["Age"],
                "Num"=>$_POST["Num"],
                "Date"=>$_POST["Date"],
                "Location"=>$_POST["Location"],
                "Email"=>$_POST["Email"],
                "Phone"=>$_POST["Phone"]
            );
            if($this->tour_model->insertReg($Params)){
                $code=1;
                $msg="Success. Thank you for book this tour.";
            }else{
                $code=-1;
                $msg="Fail. Cant book this tour.";
            }
            
        }
        echo json_encode(array("code"=>$code,"msg"=>$msg));
    }
}
